package sxe.core.cs511.team2_2;

import java.awt.image.BufferedImage;
import snobject.snImage;
import snobject.snObject;
import snobject.snObjectArgList;
import step.EvaluationFailure;
import step.FunctionWrapper;
import step.exceptions.ResourceUnavailableException;

/** OVERVIEW:  A class that defines the opcode blobdraw.  This opcode will take an snImage as an argument
 *  and return an snImage with bounding boxes drawn around each detected blob in the frame.
 *  This class is a procedural abstraction.  The abstraction function is AF(c) = {a blobDetector object}.
 *  The representation invariant is that the blobDetector object must be non-null.
 * @author lisap
 *
 */

public class blobdraw extends FunctionWrapper {
	private BlobDetector blobDetector = null;
	
	/** Constructor */
	public blobdraw(){
		blobDetector = new BlobDetector(1, false);
		
	}

	/** EFFECTS:  This method takes an snImage as an argument and returns the number of blobs in it.
	 *  REQUIRES:  The first argument to be non-null.
	 * @return the number of blobs in the image.
	 * @throws EvaluationFailure
	 */
	public snObject Call(snObjectArgList argv) throws EvaluationFailure,
			ClassCastException, ArrayIndexOutOfBoundsException,
			ResourceUnavailableException {
		
		snImage sni = argv.popImage();
		
		/* do not directly manipulate the original image, instead work with a copy */
		BufferedImage buffImg = sni.getImageCopy(); 
		BlobBag bag = blobDetector.detectBlobs(buffImg);
		BufferedImage imout =  buffImg;
		if (bag != null)
			imout = blobDetector.drawBoundingBoxes(bag);
		
		 
		return new snImage(imout);
	}

}
